package com.jackrain.nea.ad.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.ad.api.AduserattrDelCmd;
import com.jackrain.nea.ad.api.AduserattrSaveCmd;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ReferenceUtil;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.face.User;
import com.jackrain.nea.web.query.DefaultWebEvent;
import com.jackrain.nea.web.query.QuerySessionImpl;
import com.jackrain.nea.web.security.Security4Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Api(value = "子系统权限",description = "用户环境变量")
@RestController
public class AduserattrCtrl {
    @ApiOperation(value = "用户环境变量的添加以及修改功能")
    @RequestMapping(path = "/api/userattrcmdimpl/save",method = RequestMethod.POST)
    public JSONObject save(HttpServletRequest request, @RequestParam(value="param",required = true) String param){
        User user = (User) Security4Utils.getUser("root");
        QuerySessionImpl querySession = new QuerySessionImpl(user);
        DefaultWebEvent event = new DefaultWebEvent("userattrsave",request,false);
        event.put("param", JSON.parseObject(param));
        querySession.setEvent(event);
        Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.ad.api.AduserattrSaveCmd", "ad", "1.0");
        ValueHolder result;
        try{
            result = (ValueHolder)((AduserattrSaveCmd)o).execute(querySession);
            return result.toJSONObject();
        }catch (Exception e){
            result = new ValueHolder();
            result.put("code",-1);
            result.put("message",e.getMessage());
            return result.toJSONObject();
        }
    }


    @ApiOperation(value = "用户环境变量的删除功能")
    @RequestMapping(path = "/api/userattrcmdimpl/del",method = RequestMethod.POST)
    public JSONObject del(HttpServletRequest request, @RequestParam(value="param",required = true) String param){
        User user = (User) Security4Utils.getUser("root");
        QuerySessionImpl querySession = new QuerySessionImpl(user);
        DefaultWebEvent event = new DefaultWebEvent("userattrDel",request,false);
        event.put("param", JSON.parseObject(param));
        querySession.setEvent(event);
        Object o = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.ad.api.AduserattrDelCmd", "ad", "1.0");
        ValueHolder result;
        try{
            result = (ValueHolder)((AduserattrDelCmd)o).execute(querySession);
            return result.toJSONObject();
        }catch (Exception e){
            result = new ValueHolder();
            result.put("code",-1);
            result.put("message",e.getMessage());
            return result.toJSONObject();
        }
    }
}
